//
//  JCMessageTextContent.swift
//  JChat
//
//  Created by JIGUANG on 2017/3/9.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import MTCategory

open class JCClubActivityContent: NSObject, JCMessageContentType {
    public weak var delegate: JCMessageDelegate?
    public override init() {
        let text = "this is a test text"
        self.text = NSAttributedString(string: text)
        super.init()
    }
    public init(text: String) {
        self.text = NSAttributedString(string: text)
        super.init()
    }
    public init(attributedText: NSAttributedString) {
        self.text = attributedText
        super.init()
    }
    
    public init(_ infos: [String : Any]) {
        self.text = NSAttributedString(string: "测试红包消息啦")
        self.infos = infos
        super.init()
    }
    
    open class var viewType: JCMessageContentViewType.Type {
        return JCClubActivityContentView.self
    }
    open var layoutMargins: UIEdgeInsets = .init(top: 9, left: 10, bottom: 9, right: 10)
    
    open var text: NSAttributedString
    
    open var infos: [String : Any]?
    
    open func sizeThatFits(_ size: CGSize) -> CGSize {
        
        let min: CGFloat = 213 - 61 - 20
        if let desc: String = infos?["activityInfo"] as? String {
            let mattr = NSMutableAttributedString(string: desc)
            
            let paraStyle = NSMutableParagraphStyle()
            
            paraStyle.lineSpacing = 5
            
            let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCRegularFont(12),
                                                               NSAttributedString.Key.paragraphStyle : paraStyle]
            
            mattr.addAttributes(attributes, range: NSMakeRange(0, mattr.length))
            
            let mattrSize = mattr.boundingRect(with: CGSize(width: 232, height: Double(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
            
            return .init(width: 232, height: min + mattrSize.height)
        }
        return .init(width: 232, height: min)
        
//        let min: CGFloat = 213 - 61 - 20
//        if let desc: String = infos?["activityInfo"] as? String {
//            let mattr = NSMutableAttributedString(string: desc)
//
//            let paraStyle = NSMutableParagraphStyle()
//
//            paraStyle.lineSpacing = 5
//
//            let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCRegularFont(12),
//                                                               NSAttributedString.Key.paragraphStyle : paraStyle]
//
//            mattr.addAttributes(attributes, range: NSMakeRange(0, mattr.length))
//
//            let mattrSize = mattr.boundingRect(with: CGSize(width: 232, height: Double(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
//
//            return .init(width: 232, height: min + mattrSize.height)
//        }
//        return .init(width: 232, height: min)
        
        /**
         let min: CGFloat = 213 - 61 - 20
         if let desc: String = infos?["activityInfo"] as? String {
             let mattr = NSMutableAttributedString(string: desc)
             
             let paraStyle = NSMutableParagraphStyle()
             
             paraStyle.lineSpacing = 5
             
             let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCRegularFont(12),
                                                                NSAttributedString.Key.paragraphStyle : paraStyle]
             
             mattr.addAttributes(attributes, range: NSMakeRange(0, mattr.length))
             
             let mattrSize = mattr.boundingRect(with: CGSize(width: 232, height: Double(MAXFLOAT)), options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil)
             
             return .init(width: 232, height: max(213, mattrSize.height + min))
         }
         return .init(width: 232, height: 213)
         */
    }
}
